package net.hn.hnms.biz.plan.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.plan.domain.ExecutionInventory;
import net.hn.hnms.biz.plan.mapper.ExecutionInventoryMapper;
import net.hn.hnms.biz.plan.request.ExecutionInventoryReq;
import net.hn.hnms.biz.plan.service.IExecutionInventoryService;
import net.hn.hnms.biz.plan.vo.ExecutionInventoryVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@DS("master")
public class ExecutionInventoryServiceImpl extends BaseService<ExecutionInventory, ExecutionInventoryVo> implements IExecutionInventoryService {

    @Resource
    private ExecutionInventoryMapper baseMapper;

    @Override
    public int insert(ExecutionInventoryReq executionInventory) {
        ExecutionInventory insertObj = new ExecutionInventory();
        BeanUtil.copyProperties(executionInventory, insertObj);
        return baseMapper.insert(insertObj);
    }

    @Override
    public void deleteBatch(List<String> idList) {
        baseMapper.deleteBatchIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(ExecutionInventoryReq executionInventory) {
        if (StrUtil.isBlank(executionInventory.getId())) {
            throw new HuaNengException(Constants.FAIL, "id不能为空");
        }
        ExecutionInventory updateObj = new ExecutionInventory();
        BeanUtil.copyProperties(executionInventory, updateObj);
        int updateCount = baseMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public ResponsePage<ExecutionInventoryVo> selectPageList(ExecutionInventoryReq executionInventory, PageQuery pageQuery) {
        Page<ExecutionInventory> page = baseMapper.selectPage(pageQuery.build(), buildQuery(executionInventory).orderByDesc(ExecutionInventory::getInventoryDate)
                .orderByDesc(ExecutionInventory::getCreateTime));
        List<ExecutionInventoryVo> list = page.getRecords().stream().map(o -> convertWithUser(o, ExecutionInventoryVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(list, page.getTotal());
    }

    @Override
    public ExecutionInventoryVo selectDetailById(String id) {
        ExecutionInventory executionInfo = baseMapper.selectById(id);
        if (executionInfo == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return convertWithUser(executionInfo, ExecutionInventoryVo.class);
    }

    /**
     * 构造查询条件
     *
     * @param req
     * @return LambdaQueryWrapper<ExecutionInventory> 查询条件
     */
    private LambdaQueryWrapper<ExecutionInventory> buildQuery(ExecutionInventoryReq req) {
        LambdaQueryWrapper<ExecutionInventory> lqw = new LambdaQueryWrapper<ExecutionInventory>()
                .ge(req.getBeginDateTime() != null, ExecutionInventory::getInventoryDate, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, ExecutionInventory::getInventoryDate, req.getEndDateTime())
                .and(o -> o.eq(req.getMineCode() != null, ExecutionInventory::getMineCode, req.getMineCode())
                        .or()
                        .eq(req.getCompanyCode() != null, ExecutionInventory::getCompanyCode, req.getCompanyCode())
                );
        return lqw;
    }


}
